package 力扣链表;

public class Leedcode25K个一组反转链表 {
    public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null || head.next == null) return head;
        ListNode tail = head;
        for(int i = 0 ;i < k ;i++){
            if(tail == null) return head;
            tail = tail.next;
        }
        ListNode newnode = nizhuan(head,tail);
        head.next = reverseKGroup(tail,k);
        return newnode;
    }
    public ListNode nizhuan (ListNode head,ListNode endNode){
        ListNode cur = head;
        ListNode temp = null;
        ListNode node = new ListNode();
        while(cur != endNode){
            temp = cur.next;
            cur.next = node.next;
            node.next = cur;
            cur = temp;
        }
        return node.next;
    }
}
